package communication.hall
{

	import com.netease.protobuf.Message;
	import com.netease.protobuf.UInt64;
	
	import flash.utils.ByteArray;
	
	import communication.CertManager;
	
	import protobuf.HSPKG_CHANGE_AVATAR_REQ;
	import protobuf.HSPKG_CHANGE_SEX_REQ;
	import protobuf.HSPKG_DEL_MAIL_REQ;
	import protobuf.HSPKG_MAIL_COUNT_REQ;
	import protobuf.HSPKG_NAME_NICKNAME_REQ;
	import protobuf.HSPKG_NEXT_PAGE_MAIL_REQ;
	import protobuf.HSPKG_OTHERDATA_REQ;
	import protobuf.HSPKG_QUERY_TABLE_INFO_REQ;
	import protobuf.HSPKG_RECORD_REQ;
	import protobuf.HSPKG_SEARCH_RECORD_REQ;
	import protobuf.HSPKG_SELECT_TABLE_REQ;
	import protobuf.HSPKG_TNMT_DETAIL_REQ;
	import protobuf.HSPKG_TNMT_LIST_REQ;
	import protobuf.HSPKG_TNMT_PLAYERLIST_REQ;
	import protobuf.HSPKG_TNMT_REG_PLAYER_REQ;
	import protobuf.HSPKG_TNMT_TABLELIST_REQ;
	import protobuf.HSPKG_TNMT_UNREG_PLAYER_REQ;
	import protobuf.MATCH_RECORD_MESSAGE;
	import protobuf.PKG_AUTH_CHALLENGE_RPL;
	import protobuf.PKG_AUTH_REQ;

	/**
	 * 大厅数据发送者
	 * @author xumin.xu
	 *
	 */
	public class HallMsgSender
	{
		private static var _instance:HallMsgSender;

		public static function get instance():HallMsgSender
		{
			return _instance;
		}

		public static function init():void
		{
			if(_instance == null)
			{
				_instance=new HallMsgSender();
			}
		}

		private function get hallSocket():HallSocket
		{
			return HallSocket.instance;
		}

		private function get certManager():CertManager
		{
			return CertManager.getInstance();
		}

		/**
		 * 向服务器发送消息
		 * @param msgId 协议号
		 * @param message 协议内容
		 *
		 */
		private function sendMessage(msgId:uint, message:Message):void
		{
			if (hallSocket)
			{
				hallSocket.send(msgId, message);
			}
		}

		/**
		 * 连接大厅服
		 */
		public function connectHallServer():void
		{
			logger("连接大厅服务器");
			if (hallSocket == null || hallSocket.hasConnected())
				return;
			var authRequest:PKG_AUTH_REQ=new PKG_AUTH_REQ();
			authRequest.clientCert=certManager.getClientSessionCert();
			hallSocket.authRequest=authRequest;
			hallSocket.connect();
		}

		/**
		 *将保存的CLIENT_SESSION_CERT发送到服务端
		 */
		public function sendSessionCert(authRequest:Message):void
		{
			logger("向发送服务器验证身份协议");

			sendMessage(HSProto.ID_PKG_AUTH_REQ, authRequest);
		}

		/**
		 * 发送挑战码
		 */
		public function sendChallenge(challengeBytes:ByteArray):void
		{
			logger("发送挑战码");

			var signBytes:ByteArray=new ByteArray();
			certManager.sign(challengeBytes, signBytes, challengeBytes.length);
			var sign:PKG_AUTH_CHALLENGE_RPL=new PKG_AUTH_CHALLENGE_RPL();
			sign.signature=signBytes;

			sendMessage(HSProto.ID_PKG_AUTH_CHALLENGE_RPL, sign);
		}

		/**
		 * 发送桌子列表请求
		 */
		public function sendTableList():void
		{
			//logger("发送桌子列表请求");
			var req:HSPKG_SELECT_TABLE_REQ=new HSPKG_SELECT_TABLE_REQ();

			//请求现金桌子列表信息
			req.where=certManager.convertStringToByteArray("type=1");

			sendMessage(HSProto.HSID_SELECT_TABLE_REQ, req);
		}

		/**
		 * 客户端请求起匿称
		 **/
		public function OnRecvNameNicknameReq(nickName:String):void
		{
			logger("客户端请求起匿称");

			var nickNameReq:HSPKG_NAME_NICKNAME_REQ=new HSPKG_NAME_NICKNAME_REQ();
			nickNameReq.nickName=certManager.convertStringToByteArray(nickName);

			sendMessage(HSProto.HSID_NAME_NICKNAME_REQ, nickNameReq);
		}

		/**
		 * 用户请求更改性别
		 **/
		public function OnRecvChangeSexReq(sex:uint):void
		{
			logger("用户请求更改性别");

			var changeSexReq:HSPKG_CHANGE_SEX_REQ=new HSPKG_CHANGE_SEX_REQ();
			changeSexReq.sex=sex;
			sendMessage(HSProto.HSID_CHANGE_SEX_REQ, changeSexReq);
		}

		/**
		 * 用户请求更改头像
		 **/
		public function OnRecvChangeAvatarReq(avatarId:uint):void
		{
			logger(" 用户请求更改头像");

			var changeAvatarReq:HSPKG_CHANGE_AVATAR_REQ=new HSPKG_CHANGE_AVATAR_REQ();
			changeAvatarReq.avatarId=avatarId;
			sendMessage(HSProto.HSID_CHANGE_AVATAR_REQ, changeAvatarReq);
		}

		/**
		 * 请求锦标赛列表
		 */
		public function OnRecvTnmtListReq():void
		{
			logger("请求锦标赛列表");
			var req:HSPKG_TNMT_LIST_REQ=new HSPKG_TNMT_LIST_REQ();
			req.page=1;

			sendMessage(HSProto.HSID_TNMT_LIST_REQ, req);

		}

		/**
		 * 请求锦标赛详细信息
		 * @param tournamentId
		 * @param type
		 *
		 */
		public function OnRecvTnmtDetailReq(tournamentId:UInt64, type:int):void
		{
			logger("请求锦标赛详细信息");

			var tnmtDetailReq:HSPKG_TNMT_DETAIL_REQ=new HSPKG_TNMT_DETAIL_REQ();
			tnmtDetailReq.tournamentId=tournamentId;
			tnmtDetailReq.type=type;

			sendMessage(HSProto.HSID_TNMT_DETAIL_REQ, tnmtDetailReq);

		}

		/**
		 * 请求锦标赛玩家列表
		 * @param tournamentId 锦标赛编号
		 * @param page 页码
		 * @param count 单页数量
		 * @param type 锦标赛类型
		 */
		public function OnRecvTnmtPlayerlistReq(tournamentId:UInt64, page:int, count:uint, type:uint):void
		{
			logger("请求锦标赛玩家列表");

			var tnmtPlayerlistReq:HSPKG_TNMT_PLAYERLIST_REQ=new HSPKG_TNMT_PLAYERLIST_REQ();
			tnmtPlayerlistReq.tournamentId=tournamentId;
			tnmtPlayerlistReq.startpos=(page - 1) * count;
			tnmtPlayerlistReq.count=count;
			tnmtPlayerlistReq.type=type;

			sendMessage(HSProto.HSID_TNMT_PLAYERLIST_REQ, tnmtPlayerlistReq);

		}

		/**
		 * 请求某锦标赛桌子列表
		 **/
		public function OnRecvTnmtTablelistReq(tournamentId:UInt64, page:uint, type:uint):void
		{
			logger("请求某锦标赛桌子列表");

			var tnmtTablelistReq:HSPKG_TNMT_TABLELIST_REQ=new HSPKG_TNMT_TABLELIST_REQ();
			tnmtTablelistReq.tournamentId=tournamentId;
			tnmtTablelistReq.page=page;
			tnmtTablelistReq.type=type;

			sendMessage(HSProto.HSID_TNMT_TABLELIST_REQ, tnmtTablelistReq);
		}

		/**
		 * 请求注册某锦标赛
		 **/
		public function OnRecvTnmtRegPlayerReq(tournamentId:UInt64,type:int):void
		{
			logger("请求注册某锦标赛：" + tournamentId.toString());

			var registerReq:HSPKG_TNMT_REG_PLAYER_REQ=new HSPKG_TNMT_REG_PLAYER_REQ();
			registerReq.tournamentId=tournamentId;
			registerReq.type = type;

			sendMessage(HSProto.HSID_TNMT_REG_PLAYER_REQ, registerReq);
		}

		/**
		 * 请求移除注册某锦标赛
		 **/
		public function OnRecvTnmtUnRegPlayerReq(tournamentId:UInt64,type:int):void
		{
			logger("请求移除注册某锦标赛：" + tournamentId.toString());

			var unRegisterReq:HSPKG_TNMT_UNREG_PLAYER_REQ=new HSPKG_TNMT_UNREG_PLAYER_REQ();
			unRegisterReq.tournamentId=tournamentId;
			unRegisterReq.type = type;

			sendMessage(HSProto.HSID_TNMT_UNREG_PLAYER_REQ, unRegisterReq);

		}

		/**
		 * 请求(其他数据)服务器时间、在线人数、在座玩家数
		 */
		public function OnRecvOtherDataReq():void
		{
			var otherDataReq:HSPKG_OTHERDATA_REQ=new HSPKG_OTHERDATA_REQ();
			sendMessage(HSProto.HSID_OTHERDATA_REQ, otherDataReq);
			//logger("请求(其他数据)服务器时间、在线人数、在座玩家数");
		}
		
		/**
		 * 请求当前桌玩家信息 
		 */		
		public function OnRecvQueryTableInfoReq(tableId:UInt64):void
		{
			var tablePlayerInfoReq:HSPKG_QUERY_TABLE_INFO_REQ = new HSPKG_QUERY_TABLE_INFO_REQ();
			tablePlayerInfoReq.tableId=tableId;
			sendMessage(HSProto.HSID_QUERY_TABLE_INFO_REQ, tablePlayerInfoReq);
		}
		
		/**
		 * 请求邮件数量 
		 */		
		public function OnRecvMailCountReq():void
		{
			var mailCountReq:HSPKG_MAIL_COUNT_REQ = new HSPKG_MAIL_COUNT_REQ();
			sendMessage(HSProto.HSID_MAIL_COUNT_REQ, mailCountReq);
		}
		
		/**
		 * 请求部分邮件
		 *  required uint32 start_index = 1;
      	 *  required uint32 count       = 2; 
		 */		
		public function OnRecvNextPageMailReq(start_index:int, count:int):void
		{
			var mailReq:HSPKG_NEXT_PAGE_MAIL_REQ = new HSPKG_NEXT_PAGE_MAIL_REQ();
			mailReq.startIndex = start_index;
			mailReq.count = count;
			sendMessage(HSProto.HSID_NEXT_PAGE_MAIL_REQ, mailReq);
		}
		
		/**
		 * 请求删除邮件 
		 * repeated int64 id_list = 1;
		 */		
		public function OnRecvDelMailReq(id_list:Array):void
		{
		    var mailDeleteReq:HSPKG_DEL_MAIL_REQ = new HSPKG_DEL_MAIL_REQ();
		    mailDeleteReq.idList = id_list;
			sendMessage(HSProto.HSID_DEL_MAIL_REQ, mailDeleteReq);
		}
		
		/**
		 * 请求查询回放记录 
		 * @param startTime 开始时间
		 * @param endTime 结束时间
		 * @param tableType 桌子类型	1、cash，2、tournament，3，sng
		 */		
		public function OnRecvSearchRecordReq(startTime:int,endTime:int,tableType:int):void
		{
			trace("请求查询回放记录 ");
			var req:HSPKG_SEARCH_RECORD_REQ = new HSPKG_SEARCH_RECORD_REQ();
			req.startTime = startTime;
			req.endTime = endTime;
			req.tableType = tableType;
			sendMessage(HSProto.HSID_SEARCH_RECORD_REQ,req);
		}
		
		public function OnRecvRecordReq(matchRecord:MATCH_RECORD_MESSAGE):void
		{
			var req:HSPKG_RECORD_REQ = new HSPKG_RECORD_REQ();
			req.time = matchRecord.time;
			req.tableId = matchRecord.tableId;
			req.pipeId = matchRecord.pipeId;
			
			sendMessage(HSProto.HSID_RECORD_REQ,req);
		}
	}
}
